#include "input_file_manager.h"
#include <dirent.h>
/*--------------------------------------------------------------------------------*/
enum LOCAL_FILE_TYPE
{
	LOCAL_FILE_TYPE_Directory = 4,
	LOCAL_FILE_TYPE_Regular = 8
};
/*--------------------------------------------------------------------------------*/
IMPLEMENT_SINGLETON( INPUT_FILE_MANAGER );
/*--------------------------------------------------------------------------------*/
VOID INPUT_FILE_MANAGER::MountDirectory( const TEXT & path, const BOOL recursive )
{
	TEXT
		file_name;
	LOCAL_FILE_TYPE
		file_type;
	DIR
		*dpath;
	dirent
		*pfile;

	dpath = opendir( path.GetArray() );

	DEBUG_Require( dpath != NULL );

	while( ( pfile = readdir( dpath ) ) )
	{
		file_name = pfile->d_name;
		file_type = (LOCAL_FILE_TYPE) pfile->d_type;

		if( file_name != "." && file_name != ".." )
		{
			if( file_type == LOCAL_FILE_TYPE_Directory && recursive )
			{
				MountDirectory( path + "/" + file_name, true );
			}
			else if( file_type == LOCAL_FILE_TYPE_Regular )
			{
				INPUT_FILE::REF
					file;

				file.New();

				file->SetFilePath( path + "/" + file_name );
				
				if( FileTable.HasKey( file->GetFileName() ) )
				{
					DEBUG_LogLine( "WARNING: File name duplicated for \"" + file->GetBaseName() + "." + file->GetExtension() + "\", skipping." );
				}
				else
				{
					FileTable.Add( file->GetFileName(), file );
					DEBUG_LogLine( "File \"" + file->GetBaseName() + "." + file->GetExtension() + "\" found." );
				}
			}
		}
	}

	closedir( dpath );
}
/*--------------------------------------------------------------------------------*/

